﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.sampler.NewObjectSample;
	
	/** ShapeProduction Task imports test file and sets up a new task */
	public class ShapeProductionTask extends MovieClip
	{
		// public static variables
		public static var fps:Number;
		public static var unit:Number;
		public static var TYPES:Array;
		// public variables
		public var userID:String, projectID:String;
		public var buildMode:Boolean;
		// public objects
		public var ixml:XML, oxml:XML;
		public var ofile:File;
		// Private objects
		private var idScreen:WelcomeScreen;
		private var openFile:OpenXMLFile;
		private var saveFile:SaveXMLFile;
		private var task:Task;
	
		/** Constructor sets up a new idscreen*/
		public function ShapeProductionTask ( )
		{
			this.addEventListener(Event.ADDED_TO_STAGE, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			trace(stage.stageWidth, stage.stageHeight);
			this.removeEventListener(Event.ADDED_TO_STAGE, handleAdded);
			fps = stage.frameRate;
			unit = stage.stageWidth/1000;
			// new task
			//this.graphics.drawRect(0,0,this.stage.stageWidth, this.stage.stageHeight);
			this.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			
			TYPES = new Array("dodecagon", "decagon", "nonagon", "enneagon", "octagon", "heptagon", "hexagon", "pentagon", "quadrilateral", "quadrangle", "convex_dodecagon", "convex_decagon", "convex_nonagon", "convex_enneagon", "convex_octagon", "convex_heptagon", "convex_hexagon", "convex_pentagon", "convex_quadrilateral", "convex_quadrangle", "concave_dodecagon", "concave_decagon", "concave_nonagon", "concave_enneagon", "concave_octagon", "concave_heptagon", "concave_hexagon", "concave_pentagon", "concave_quadrilateral", "concave_quadrangle", "trapezoid", "isosceles_trapezoid", "right_trapezoid", "isosceles_right_trapezoid", "kite", "parallelogram", "rhombus", "rectangle", "square", "regular_quadrilateral", "regular_quadrangle", "triangle", "isosceles_triangle", "equilateral_triangle","regular_triangle", "scalene_triangle", "right_triangle", "obtuse_triangle", "acute_triangle", "scalene_acute_triangle", "acute_scalene_triangle", "right_isosceles_triangle", "isosceles_right_triangle", "scalene_right_triangle", "right_scalene_triangle", "isosceles_obtuse_triangle","obtuse_isosceles_triangle", "acute_isosceles_triangle", "obtuse_scalene_triangle");
			
			idScreen = new WelcomeScreen("My Shape Garden","Please enter your name.",0, "Please enter the name of your project.");
			idScreen.x = 0;
			idScreen.y = 0;
			idScreen.addEventListener(Event.COMPLETE, handleIdComplete);
			this.addChild(idScreen);
			
			this.addEventListener(Event.RESIZE, handleResize);
		}
		/** Called once id is complete, lookup xml file */
		private function handleIdComplete (event:Event):void
		{
			idScreen.removeEventListener(Event.COMPLETE, handleIdComplete);
			userID = idScreen.text1;
			if (userID.substr(0,1)=="_")
			{
				userID = userID.substr(1);
				buildMode = true;
			} else
			{
				buildMode = false;
			}
			projectID = idScreen.text2;
			this.removeChild(idScreen);
			
			// Now look for the user's file or a general project file
			openFile = new OpenXMLFile(userID, projectID);
			openFile.addEventListener(Event.COMPLETE, handleOpenXMLFile);
			this.addChild(openFile);
		}
		/** Gets the xml and other related variables */
		private function handleOpenXMLFile(event:Event):void
		{
			openFile.addEventListener(Event.COMPLETE, handleOpenXMLFile);
			ixml = openFile.xml;
			ofile = openFile.file;
			oxml = ixml.copy();
			this.removeChild(openFile);
						
			task = new Task(oxml, buildMode);
			task.addEventListener(Event.COMPLETE, handleTaskComplete);
			this.addChild(task);
		}
		private function handleTaskComplete (event:Event):void
		{
			task.removeEventListener(Event.COMPLETE, handleTaskComplete);
			saveFile = new SaveXMLFile (ofile, userID, projectID, oxml);
			saveFile.addEventListener(Event.COMPLETE, _handleTaskComplete);
			this.addChild(saveFile);
		}
			private function _handleTaskComplete(event:Event):void
			{
				saveFile.removeEventListener(Event.COMPLETE, _handleTaskComplete);
				this.stage.nativeWindow.close();
			}
		/** Save the current state of the xml */	
		public function saveXML():SaveXMLFile
		{
			saveFile = new SaveXMLFile (ofile, userID, projectID, oxml);
			//saveFile.addEventListener(Event.COMPLETE, _saveXML);
			this.addChild(saveFile);
			return saveFile;
		}			
		
		/** Handle resize so point sizes can change */
		private function handleResize (event:Event):void
		{
			unit = stage.stageWidth/1000;
		}
	}
}